package FlowGram.Application.CommandHost.FinchActionListeners;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import FlowGram.Application.Parser.Actions.Chirp;
import FlowGram.Application.Parser.Actions.Control.Repeat;
import FlowGram.Application.Parser.Utility.ClassFinder;

public class ChirpListener extends FinchActionListener {

	JFrame frame;
	
	Chirp ChirpAct;
	
	public ChirpListener(JFrame frame){
		this.frame = frame;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		ChirpAct = new Chirp();
		while(true){

			
			String s = (String)JOptionPane.showInputDialog(
			                    frame,
			                    "Chirp frequency and duration\n"
			                    + "Format: <frequency(Hz)>,<duration(milliseconds)>",
			                    "Chirp Parameters",
			                    JOptionPane.QUESTION_MESSAGE);
			if(s == null){break;}		//exit if cancel or X is clicked
			
			int freq = -1;
			int dur = -1;
			
			try{
				
				freq = Integer.parseInt(s.substring(0, s.indexOf(",")));
				dur = Integer.parseInt(s.substring(s.indexOf(",")+1));

				System.out.println("freq " + freq + "\ndur "+dur + "\n");

				ChirpAct.setFrequency(freq);
				ChirpAct.setDuration(dur);
				
				addCode(ChirpAct);
				
				break;
			}catch(NumberFormatException numE){
				JOptionPane.showMessageDialog(frame,
			    "Please enter parameters in the format of <frequency>,<duration>");
			}
			

			
			
		}
	}
}
